﻿using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Order.Application.Data;
using Order.Infrastructure.Interceptors;
using Ordering.Infrastructure.Data;


namespace Ordering.Infrastructure
{
    public static class DependenceInjectionExtension
    {
        //此处的依赖注入来源于Block 模块
        public static IServiceCollection AddInfrastructureService(this IServiceCollection services,IConfiguration configuration)
        {
            var connectionString = configuration.GetConnectionString("Database");
            services.AddScoped<ISaveChangesInterceptor,AuditableEntityInterceptor>();
            services.AddScoped<ISaveChangesInterceptor,DomainEventInterceptor>();
            services.AddDbContext<ApplicationDbContext>((sp,options )=>
            {
                options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
                options.UseSqlServer(connectionString);
            });

            //接口来自Application层，实现类来自Infrastructure层
            services.AddScoped<IApplicationDbcontext, ApplicationDbContext>();

            return services;
        }
    }
}
